<template>
  <div class="layout">
    <div style="display: flex; justify-content: center">
      <Breadcrumb class="breadcrumb" :crumbs="crumbs"></Breadcrumb>
    </div>
    <div class="content">
      <div class="check_bar">
        <div class="item" :class="{ 'sort_1': timetype==1, 'sort_2': timetype==2 }" @click="timeBtn(timetype,1)">入驻时间</div>
        <div class="item" :class="{ 'sort_1': dividetype==1, 'sort_2': dividetype==2 }" @click="timeBtn(dividetype,2)">综合评分</div>
      </div>
      <div class="list">
        <div class="item" v-for="(item, index) in agencyList" :key="index" @click="agencyDetail(item.id)">
            <div class="msg">
              <div class="title">
                <div class="logo" :style="item.avatar?'background-color: #fff;':''">
                  <PublicImage :src="item.avatar" v-if="item.avatar"></PublicImage>
                  <span v-else>{{item.short}}</span>
                </div>
                <span>{{item.agencyName}}</span>
              </div>
              <p class="text two_text">{{item.serviceDesc}}</p>
              <div class="tags" v-if="item.serviceTypeName.length">
                <p v-for="(item, index) in item.serviceTypeName" :key="index">{{item}}</p>
              </div>
            </div>
            <div class="scroe">
              <template v-if="item.grade">
                <p class="name">综合评分</p>
                <p class="number">
                  <span class="point">{{item.grade}}</span>
                  <span class="text">分</span>
                </p>
                <div class="star">
                  <Star :size="48" :score="item && item.grade"></Star>
                </div>
              </template>
               <p v-else class="name2">暂无评分</p>
            </div>
          </div>
      </div>
      <template v-if="agencyList.length">
         <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum" background
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
      </template>
      <Buttons :buttons="buttons" @goBack="goBack"></Buttons>
    </div>
  </div>
</template>

<script>
import Breadcrumb from "@/components/breadcrumb";

import { hostqt } from "@/utils/config.js"
import { request } from "@/utils/request.js";
import { getquerySettledAgency, getqserviceRequirement } from "@/common/api/service.js";
import Star from "./components/star";

export default {
  name: "service",
  components: { Breadcrumb, Star },
  data() {
    return {
      buttons:[{text:"返回",size:"l",type:"normal",method:"goBack"}],
      pageNum: 1,
      pageSize: 10,
      total: 0,
      companyInfo: {},
      bigImage: "",
      showImgDiv: false,
      crumbs: [
        {
          label: "门户首页",
          value: "index",
        },
        {
          label: "服务市场",
          value: "service"
        },
        {
          label: "已入驻商家",
          value: "",
          choose: true,
        }
      ],
      login: false,
      pageSize: 10,
      current: 1,
      agencyList: [],
      sortKey: [],
      sortOrder: [],
      timetype: null, // 入驻时间状态 
      dividetype: null, // 综合评分
    };
  },
  methods: {
    // 跑转到商家详情页
    agencyDetail(agencyId){
      console.log('id', agencyId)
      this.$router.push({
        path: "serviceBusinessDetails",
        query: {
          agencyId,
        },
      });
    },
    // 已入驻的商家列表
    async getquerySettledAgency(){
      // let a = this.sortKey.every(item => item.indexOf(item))
      // console.log('12',a)
      // if(!a) {

      // }
      
      let obj = {
        current: this.pageNum,
        pageSize: this.pageSize,
        sortKey: this.sortKey,
        sortOrder: this.sortOrder
      }
      let res = await getquerySettledAgency(obj);
      console.log('res11', res.result.data)
      // 把评分进行四舍五入保留一位小数
      res.result.data.forEach(item => {
        if(item.grade){
          return item.grade = Math.round(item.grade * 10) / 10
        }
      })
      console.log('res22', res.result.data)
      if(res.status == 200 && res.result.data.length) {
        res.result.data.forEach(item => {
          if(item.serviceTypeName) {
            item.serviceTypeName = item.serviceTypeName.split(',')
          }
          if(!item.avatar){
            item.short = item.agencyName.substr(0,1)
          }
        });
        this.agencyList = res.result.data
        this.total = res.result.total*1
        console.log('this.total', this.total)
        // console.log('this.agencyList', this.agencyList)
      }
    },
     /**
			 * 改变页面大小事件
			 */
			handleSizeChange (e) {
				this.pageSize = e
				this.getquerySettledAgency()
			},
			/**
			 * 换页事件
			 */
			handleCurrentChange (e) {
				this.pageNum = e
				this.getquerySettledAgency()
      },
      timeBtn(state, type){
        // state当前按钮状态
        // type 判断点击是哪个按钮 1是入驻时间  2是综合评分
        if(this.timetype == 2) {
          this.timetype = null
        }
        if(this.dividetype == 2) {
          this.dividetype = null
        }
        this.sortKey = []
        this.sortOrder = []
        if(type ==1) {
          this.dividetype = null
          if(!state){
            this.timetype =1
            this.sortKey.push('gmtCreate')
            this.sortOrder.push('asc')
          } else {
            this.sortKey.push('gmtCreate')
            this.timetype = state ==1? 2: 1
            this.sortOrder[0] = state ==1? 'desc' : 'asc'
          }
          
        }else {
          this.timetype = null
          if(!state){
            this.dividetype =1
            this.sortKey.push('grade')
            this.sortOrder.push('asc')
          } else {
            this.dividetype = state ==1? 2: 1
            this.sortKey.push('grade')
           this.sortOrder[0] = state ==1? 'desc' : 'asc'
          }
        }
        this.getquerySettledAgency()
      },
     /**
			 * 返回上一页
			 */
    goBack(){
      this.$router.back();
    }
  },
  mounted() {
    // this.getquerySettledAgency()
  },
  activated() {},
  created() {
    // console.log(111);
    this.getquerySettledAgency()
  },
};
</script>

<style lang="scss" scoped>
.breadcrumb {
  width: 1200px;
  margin: 20px 0;
}
.two_text {
  word-break: break-all;
  text-overflow: ellipsis;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.content {
  padding: 10px 0 40px;
  width: 1200px;
  margin: auto;
  text-align: left;
  .check_bar {
    height: 40px;
    background: #F4F4F4;
    border: 1px solid #EEEEEE;
    font-size: 14px;
    padding: 0 14px;
    display: flex;
    align-items: center;
    cursor: pointer;
    .item {
      margin-right: 32px;
      padding-right: 15px;
      background-repeat: no-repeat;
      background-image: url(../../assets/image/service/icon_11.png);
      background-repeat: no-repeat;
      background-size: 7px;
      background-position: right center;
      &.sort_1 {
        background-image: url(../../assets/image/service/icon_8.png);
      }
      &.sort_2 {
        background-image: url(../../assets/image/service/icon_9.png);
      }
    }
  }
  .list {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    .item {
      padding: 23px 38px 35px 27px;
      border: 1px solid #EEEEEE;
      display: flex;
      margin-top: 20px;
      width: 590px;
      box-sizing: border-box;
      cursor: pointer;
      .msg {
        // flex: 1;
        width: 310px;
        .title {
          display: flex;
          align-items: center;
          font-size: 22px;
          .logo {
            padding: 5px;
            flex-shrink: 0;
            border: 1px solid #CFCFCF;
            border-radius: 8px;
            margin-right: 10px;
            width: 50px;
            height: 50px;
            box-sizing: border-box;
            text-align: center;
            line-height: 40px;
            font-weight: 700;
            font-size: 30px;
            color: #fff;
            background-color: #045DB9;
            img {
              width: 40px;
              height: 40px;
              object-fit: cover;
              display: block;
            }
          }
        }
        .text {
          font-size: 14px;
          margin: 14px 0;
        }
        .tags {
          display: flex;
          flex-wrap: wrap;
          color: #F09450;
          font-size: 14px;
          margin: -8px -8px 0 0;
          p {
            padding: 4px 7px;
            background-color: #F7F1EB;
            border-radius: 3px;
            margin: 8px 8px 0 0;
          }
        }
      }
      .scroe {
        margin-left: 40px;
        flex-shrink: 0;
        align-self: flex-end;
        text-align: center;
        width: 170px;
        .name {
          font-size: 14px;
          font-weight: bold;
        }
        .name2{
          line-height: 100px;
          height: 100px;
        }
        .number {
          color: #DB3F24;
          margin: 6px 0;
          padding-left: 27px;
          .point {
            font-size: 60px;
            font-weight: bold;
          }
          .text {
            font-size: 14px;
            margin-left: 5px;
          }
        }
        .star {
          display: flex;
          align-items: center;
          img {
            width: 24px;
            height: 24px;
            margin-left: 8px;
            display: block;
          }
        }
      }
    }
  }
  ::v-deep .el-pagination {
    text-align: right;
    margin: 40px 0 60px;
  }
  .back_btn {
    width: 120px;
    height: 40px;
    background: #F1F2F4;
    border-radius: 3px;
    color: #045DB9;
    font-size: 16px;
    line-height: 40px;
    margin: 40px auto 0;
    text-align: center;
    display: block;
  }
}

.layout {
  min-height: 100vh;
  position: relative;
  padding-bottom: 150px;
  box-sizing: border-box;
}

.footer {
  position: absolute;
  bottom: 0;
  width: 100%;
}
</style>
